package week12.chat;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

/**
 * @author The setting sun
 * @version 1.0
 * @title: Client
 * @description: 客户端
 * @date 2021/11/24 8:55
 */
public class Client {
    public static void main(String[] args) {
        new Client().init();
    }

    JFrame frame = new JFrame("聊天室");

    JPanel p1 = new JPanel();
    JLabel usernameLabel = new JLabel("用户名：");
    JTextField usernameText = new JTextField(16);
    JButton connectionButton = new JButton("连接");

    JPanel p2 = new JPanel();
    JTextArea showInfoPanel = new JTextArea(17, 36);
    JScrollPane scrollPane = new JScrollPane(showInfoPanel);

    JPanel p3 = new JPanel();
    JButton sendButton = new JButton("发送");
    JTextField sendText = new JTextField(35);

    Socket socket;

    public void init() {
        operation();
        showInfoPanel.setFont(new Font("微软雅黑", Font.BOLD, 16));
        showInfoPanel.setBackground(Color.RED);
        showInfoPanel.setForeground(Color.blue);

        sendButton.setEnabled(false);
        sendText.setEnabled(false);
        showInfoPanel.setEnabled(false);

        p1.add(usernameLabel);
        p1.add(usernameText);
        p1.add(connectionButton);

        p2.add(scrollPane);

        p3.add(sendText);
        p3.add(sendButton);

        frame.add(p1, BorderLayout.NORTH);
        frame.add(p2, BorderLayout.CENTER);
        frame.add(p3, BorderLayout.SOUTH);

        frame.setVisible(true);
        frame.setResizable(false);
        frame.setSize(600, 500);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private void operation() {
        connectionButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    socket = new Socket(InetAddress.getLocalHost(), 7777);
                    String username = usernameText.getText();
                    System.out.println(username + "发起连接");

                    OutputStream os = socket.getOutputStream();
                    DataOutputStream dos = new DataOutputStream(os);

                    dos.writeUTF(username);
                    System.out.println("连接成功");

                    String user = usernameText.getText();
                    frame.setTitle(user + "的聊天框");

                    sendButton.setEnabled(true);
                    sendText.setEnabled(true);

                    new ClientThread(socket, showInfoPanel, username).start();
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
            }
        });

        sendText.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    try {
                        InputStream is = socket.getInputStream();
                        DataInputStream dis = new DataInputStream(is);
                        OutputStream os = socket.getOutputStream();
                        DataOutputStream dos = new DataOutputStream(os);

                        String text = sendText.getText();
                        dos.writeUTF(text);
                        showInfoPanel.append("我：" + text + "\n");
                        sendText.setText("");
                    } catch (IOException ioException) {
                        ioException.printStackTrace();
                    }
                }
            }
        });

        sendButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    InputStream is = socket.getInputStream();
                    DataInputStream dis = new DataInputStream(is);
                    OutputStream os = socket.getOutputStream();
                    DataOutputStream dos = new DataOutputStream(os);

                    String text = sendText.getText();
                    dos.writeUTF(text);
                    showInfoPanel.append("我：" + text + "\n");
                    sendText.setText("");
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
            }
        });

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                try {

                    socket.close();
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
            }
        });
    }
}

class ClientThread extends Thread {
    Socket socket;
    JTextArea showInfoPanel;
    String username;
    Boolean isOnline = true;

    public ClientThread(Socket socket, JTextArea area, String username) {
        this.socket = socket;
        this.showInfoPanel = area;
        this.username = username;
    }

    @Override
    public void run() {
        try {
            DataInputStream dis = new DataInputStream(socket.getInputStream());

            while (isOnline) {
                String text = dis.readUTF();
                showInfoPanel.append(text + "\n");
            }
        } catch (IOException e) {
            isOnline = false;
            e.printStackTrace();
        }
    }
}
